package com.example.android.xxm;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.android.xxm.adapter.CategoryAdapter;
import com.example.android.xxm.adapter.CategoryGridAdapter;
import com.example.android.xxm.model.CategoryGridModel;
import com.example.android.xxm.model.CategoryModel;
import com.example.android.xxm.utils.ToolUtil;

import java.util.ArrayList;

/**
 * Created by 117 on 2017/11/29.
 */

public class CategoryFragment extends Fragment {
    private ListView list_category;
    private GridView grid_category;
    private EditText ed_soushuo;
    private String[] name = { "新品推荐", "水果", "蔬菜", "肉禽蛋类", "水产海鲜", "熟食面点", "粮油副食", "烘培乳品", "休闲食品", "酒水茶饮" , "厨房用品", "礼品礼盒"};
    private ArrayList<CategoryModel> list;
    private ArrayList<CategoryGridModel> gridlist;
    private int[] img_name = { R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
            R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
            R.mipmap.ic_launcher };
    private String[] txt_name = { "多彩心率智能手环1", "多彩心率智能手环2", "多彩心率智能手环3", "多彩心率智能手环4", "多彩心率智能手环5", "多彩心率智能手环6", "多彩心率智能手环7",
            "多彩心率智能手环" };
    private View view2;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.activity_category, null);
        initView(view);
        return view;
    }

    private void initView(View view) {
        view2 = view;
        initUi(view);
        initData(view);
        initEvent(view);
    }

    // 初始化控件
    private void initUi(View view) {
        list_category = (ListView) view.findViewById(R.id.list_category);
        grid_category = (GridView) view.findViewById(R.id.grid_category);
        list_category.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                TextView txt = (TextView) arg1.findViewById(R.id.txt_category_list);
                String name = txt.getText().toString();
                ArrayList<CategoryGridModel> gridlist2 = new ArrayList<CategoryGridModel>();
                if (name.equals("新品推荐")) {
                    for (int i = 0; i < img_name.length; i++) {
                        CategoryGridModel model = new CategoryGridModel();
                        model.setImg_grid(ToolUtil.getPropThumnail(img_name[i], view2.getContext()));
                        model.setTxt_name(txt_name[i]);
                        gridlist2.add(model);
                    }
                } else {
                    for (int i = 0; i < img_name.length; i++) {
                        CategoryGridModel model = new CategoryGridModel();
                        model.setImg_grid(ToolUtil.getPropThumnail(img_name[i], view2.getContext()));
                        model.setTxt_name(txt_name[i] + i);
                        gridlist2.add(model);
                    }
                }
                CategoryGridAdapter categoryGridAdapter = new CategoryGridAdapter(view2.getContext(), gridlist2);
                grid_category.setAdapter(categoryGridAdapter);
            }
        });
        grid_category.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                //Intent intent = new Intent(arg1.getContext(), ProductDetailsActivity.class);
                //startActivity(intent);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    // 数据
    private void initData(View view) {
        list = new ArrayList<CategoryModel>();
        for (int i = 0; i < name.length; i++) {
            CategoryModel model = new CategoryModel();
            model.setName(name[i]);
            list.add(model);
        }
        gridlist = new ArrayList<CategoryGridModel>();
        for (int i = 0; i < img_name.length; i++) {
            CategoryGridModel model = new CategoryGridModel();
            model.setImg_grid(ToolUtil.getPropThumnail(img_name[i], view.getContext()));
            model.setTxt_name(txt_name[i]);
            gridlist.add(model);
        }
    }

    // 事件
    private void initEvent(View view) {
        CategoryAdapter categoryAdapter = new CategoryAdapter(view.getContext(), list);
        list_category.setAdapter(categoryAdapter);
        CategoryGridAdapter categoryGridAdapter = new CategoryGridAdapter(view.getContext(), gridlist);
        grid_category.setAdapter(categoryGridAdapter);
    }

}

